// service/LoginService.java
package service.Login;

import backend.GeneralDatabase;
import backend.EmptyOutput;
import backend.PermissionDenied;

public class LoginService {
    private final GeneralDatabase database;

    public LoginService(GeneralDatabase database) {
        this.database = database;
    }

    public LoginResult authenticate(String username, String password) {
        try {
            String result = database.tryLogin(username, password);
            int permission = Integer.parseInt(result);
            return LoginResult.success(permission);
        } catch (EmptyOutput | NumberFormatException e) {
            return LoginResult.invalidCredentials();
        } catch (PermissionDenied e) {
            return LoginResult.permissionDenied(e.getMessage());
        } catch (Exception e) {
            return LoginResult.error("登录失败: " + e.getMessage());
        }
    }
}
